//
//  HYAudioRoomViewController.m
//  XPlayer
//
//  Created by liguoqing on 2023/9/22.
//

#import "HYAudioRoomViewController.h"
#import "HYAudioPlayAreaView.h"
#import "HYAudioSuspensionView.h"

@interface HYAudioRoomViewController ()

@property (nonatomic, assign) BOOL isFirst;

@property (nonatomic, strong) HYVideoViewModel *viewModel;
/// 配置
@property (nonatomic, strong) HYVideoPlayerConfig *config;
/// 音频播放主页
@property (nonatomic, strong) HYAudioPlayAreaView *audioPlayView;

@end

@implementation HYAudioRoomViewController

- (void)dealloc
{
    CLog(@"dealloc -- %@",[HYAudioRoomViewController class]);
}

- (instancetype)initWithViewModel:(HYVideoViewModel *)viewModel
{
    self = [super init];
    if(self){
        self.viewModel = viewModel;
        self.config = viewModel.videoConfig;
    }
    return self;
}

- (void)viewDidLoad 
{
    [super viewDidLoad];
    self.navigationBarView.titleLabel.text = @"VPlayer";
    if (self.viewControllerRemovedFromNavWhenAppered) {
        // 从栈内移除
        NSMutableArray *viewControllers = [[NSMutableArray alloc] initWithArray:self.navigationController.viewControllers];
        [viewControllers removeObject:self.viewControllerRemovedFromNavWhenAppered];
        self.navigationController.viewControllers = viewControllers;
        // 置空
        self.viewControllerRemovedFromNavWhenAppered = nil;
    }
    [self configUI];
    [self bindSignal];
}

- (void)leftButtonClick
{
    [super leftButtonClick];
    if (self.playView && self.playView.playerManager.player.playStauts) {
        [HYAudioSuspensionView showPlayer:self];
    }else{
        [self removePlayer];
    }
}

- (void)configUI
{
    // 添加播放器
    HYVideoRoomViewController *videoVC = [[HYVideoRoomViewController alloc]initWithViewModel:self.viewModel];
    self.playView = videoVC;
    [self addChildViewController:self.playView];
    [self.view addSubview:self.playView.view];
    [self.playView.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    //添加音频播放页面
    UIView *areaSuperView = [UIView new];
    areaSuperView.backgroundColor = AppBgThemeColor;
    [self.view addSubview:areaSuperView];
    [areaSuperView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.navigationBarView.mas_bottom);
        make.left.bottom.right.mas_equalTo(0);
    }];
    
    HYAudioPlayAreaView *areaView = [HYAudioPlayAreaView new];
    areaView.audioTitle = [self.config fetchCurrentVideoTitle];
    [areaSuperView addSubview:areaView];
    [areaView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.center.mas_equalTo(0);
    }];
    self.audioPlayView = areaView;
    [self.view bringSubviewToFront:self.navigationBarView];
}

- (void)bindSignal
{
    //进度条信号绑定
    [self.audioPlayView.progressView bindRoomPlayerSignal:self.playView];
    //按钮
    self.audioPlayView.playButton.rac_command = self.playView.bottomToolBar.playButton.rac_command;
    self.audioPlayView.lastButton.rac_command = self.playView.bottomToolBar.lastButton.rac_command;
    self.audioPlayView.nextButton.rac_command = self.playView.bottomToolBar.nextButton.rac_command;
    self.audioPlayView.catalogButton.rac_command = self.playView.bottomToolBar.catalogButton.rac_command;
    self.audioPlayView.speedButton.rac_command = self.playView.bottomToolBar.speedButton.rac_command;
    self.audioPlayView.collectButton.rac_command = self.playView.bottomToolBar.collectButton.rac_command;
    self.audioPlayView.downloadButton.rac_command = self.playView.bottomToolBar.downloadButton.rac_command;
    RAC(self.audioPlayView.playButton, selected) = RACObserve(self.playView.bottomToolBar.playButton, selected);
    RAC(self.audioPlayView,playing) = RACObserve(self.playView.playerManager.player, playStauts);
    RAC(self.audioPlayView.collectButton,selected) = RACObserve(self.viewModel,isCollect);
    RAC(self.audioPlayView.downloadButton,selected) = RACObserve(self.viewModel,isDownload);
    
    //绑定显示信号
    RAC(self.audioPlayView, audioTitle) = [RACObserve(self.config, videoTitle) skip:1];
    @weakify(self);
    self.audioPlayView.kjButton.rac_command = [[RACCommand alloc]initWithSignalBlock:^RACSignal *(id input) {
        @strongify(self);
        [self.playView.playerManager fastForwardAndfastBackward:15];
        return [RACSignal empty];
    }];
    self.audioPlayView.ktButton.rac_command = [[RACCommand alloc]initWithSignalBlock:^RACSignal *(id input) {
        @strongify(self);
        [self.playView.playerManager fastForwardAndfastBackward:-15];
        return [RACSignal empty];
    }];
    self.audioPlayView.playModeButton.rac_command = [[RACCommand alloc]initWithSignalBlock:^RACSignal *(id input) {
        @strongify(self);
        [self.viewModel changePlayMode];
        return [RACSignal empty];
    }];
    [[RACObserve(self.playView.playerManager.player, rate) distinctUntilChanged] subscribeNext:^(id x) {
        @strongify(self);
        NSString *speed = [NSString stringWithFormat:@"%.1f", [x floatValue]];
        [self.audioPlayView.speedButton setTitle:speed forState:UIControlStateNormal];
    }];
    [[RACObserve(self.viewModel.videoConfig, playMode) distinctUntilChanged] subscribeNext:^(id x) {
        @strongify(self);
        UIImage *image;
        if([x integerValue] == 1){
            image = [UIImage imageNamed:@"icon_yp_xh"];
        }else if ([x integerValue] == 2){
            image = [UIImage imageNamed:@"icon_yp_sj"];
        }else{
            image = [UIImage imageNamed:@"icon_yp_sx"];
        }
        [self.audioPlayView.playModeButton setImage:image forState:UIControlStateNormal];
    }];
}

/// 远程响应事件（在vc里面才响应该方法）
- (void)remoteControlReceivedWithEvent:(UIEvent *)event
{
    [self.playView.playerManager HSPControlReceivedWithEvent:event];
}

- (void)removePlayer
{
    [self.playView stop];
    [self.playView removeFromParentViewController];
    self.playView = nil;
}


@end
